package com.ogawa.chair7808.ui.presenter;


import com.easepal.socketiolib.model.SocketModel;
import com.ogawa.chair7808.observer.SocketDataObserver;
import com.ogawa.chair7808.ui.iview.IScanShoulderView7506;
import java.util.concurrent.TimeUnit;
import com.ogawa.base.Constant.CommmandNum;
import io.reactivex.Observable;
import io.reactivex.Observer;
import io.reactivex.annotations.NonNull;
import io.reactivex.disposables.Disposable;
import io.reactivex.schedulers.Schedulers;


/**
 * 肩部检测
 * Created by FHB on 2017/12/13.
 */

public class ScanShoulderPresenter7506 implements SocketDataObserver.SocketReceive {
    IScanShoulderView7506 mView;
    private boolean isScanOk;
    private boolean isToAdjust;
    private Disposable mDisposable;
    private boolean onAdjust;//没有在调整

    public ScanShoulderPresenter7506(IScanShoulderView7506 view) {
        mView = view;
        SocketDataObserver.getInst().registerObserver(this);
    }


    @Override
    public void onRceiveSocket(SocketModel model) {
        String key = model.getKey();
        String value = model.getValue();
        if(key.equals(CommmandNum.mStatusShoulderOk)) {
            if (value.equals("1")) {
                isScanOk = true;
                mView.closeActivity();
            }
        }

//        }else if(key.equals(CommmandNum.mStatusShoulderAdjust)){
//            if(value.equals("1")){
//                isToAdjust =true;
//            }
//        }
//        if(isScanOk||isToAdjust){
//            if(!onAdjust){
//                //onAdjust 用于标识以下方法时候在执行了  如果在执行了 就不用重复执行
//                onAdjust = true;
//                toAdjust();
//            }
//
//        }

    }
    public void toAdjust(){
            Observable.timer(200, TimeUnit.MILLISECONDS)
                    .subscribeOn(Schedulers.io())
                    .subscribe(new Observer<Long>() {
                        @Override
                        public void onSubscribe(@NonNull Disposable d) {

                            mDisposable =d;
                        }

                        @Override
                        public void onNext(@NonNull Long aLong) {
                            if(isToAdjust){
                                mView.startToAdjust();
                            }else {
                                mView.closeActivity();
                            }
                            isScanOk =false;
                            isToAdjust = false;
                            onAdjust = false;
                            mDisposable.dispose();
                        }

                        @Override
                        public void onError(@NonNull Throwable e) {
                        }

                        @Override
                        public void onComplete() {
                        }
                    });

    }
    public void onDestory(){
        SocketDataObserver.getInst().removeObserver(this);
    }
}
